package com.example.controller;

import com.example.bean.Venue;
import com.example.bean.VenueBooking;
import com.example.bean.VenueOrder;
import com.example.service.EquipmentService;
import com.example.service.VenueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Api(tags = "场地管理模块",description = "场地的增、删、改、查、预定")
@Controller
@RequestMapping("/venue")
public class VenueController {
    @Resource
    VenueService venueService;

    @Resource
    EquipmentService equipmentService;

//    @ApiOperation("查询全部场地")
//    @GetMapping("/queryAllVenue")
//    public String queryAllVenue(@RequestParam(value = "pageNum", required = false,defaultValue = "1")Integer pageNum,
//                                @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
//                                Model model){
//        PageInfo<Venue> venueList = venueService.queryAllVenue(pageNum,pageSize);
//        model.addAttribute("venueList",venueList);
//        return"Venue/venueList";
//    }

//    @ApiOperation("通过名字查询场地")
//    @GetMapping("/queryVenueByName/{venueName}")
//    public String queryVenueByName(@RequestParam(value = "pageNum", required = false,defaultValue = "1")Integer pageNum,
//                                   @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
//                                   Model model,@PathVariable String venueName){
//        PageInfo<Venue> venueList = venueService.queryVenueByName(pageNum,pageSize,venueName);
//        model.addAttribute("venueList",venueList);
//        return "Venue/venueList";
//    }

    @ApiOperation("通过名字查询场地")
    @GetMapping("/queryVenueByName")
    @ResponseBody
    public List<Venue> queryVenueByName(@RequestParam("venueName") String venueName){
        return venueService.queryVenueByName(venueName);
    }

    @ApiOperation("通过用户id查询场地")
    @GetMapping("/queryVenueByUserId")
    @ResponseBody
    public List<Venue> queryVenueByUserId(@RequestParam("userId") int userId){
        return venueService.queryVenueByUserId(userId);
    }

//    @ApiOperation("添加场地页面")
//    @GetMapping("/toAddVenue")
//    public String toAddVenue(Model model,String name){
//        List<Equipment> equipments = equipmentService.chargeEquipmentByUsername(name);
//        model.addAttribute("equipments",equipments);
//        return "venue/addVenue";
//    }

    @ApiOperation("保存场地")
    @PostMapping("/addVenue")
    @ResponseBody
    public int addVenue(@RequestBody Venue venue){
        return venueService.addVenue(venue);
    }

    @ApiOperation("删除场地")
    @PostMapping("/deleteVenue")
    @ResponseBody
    public boolean deleteVenue(int venueId){
        return venueService.deleteVenue(venueId);
    }

//    @ApiOperation("修改场地页面")
//    @GetMapping("/toUpdateVenue/{venueId}")
//    public String toUpdateVenue(@PathVariable int venueId, Model model){
//        Venue venue = venueService.queryVenueById(venueId);
//        model.addAttribute("venue",venue);
//        return "venue/updateVenue";
//    }

    @ApiOperation("修改场地")
    @PostMapping("/updateVenue")
    @ResponseBody
    public int updateVenue(@RequestBody Venue venue){
        return venueService.updateVenue(venue);
    }

    @ApiOperation("修改场地状态")
    @PostMapping("/updateVenueState")
    @ResponseBody
    public int updateVenueState(@RequestParam("venueId") int venueId, @RequestParam("state") int state, @RequestParam("userId") int userId){
        Venue venue = venueService.queryVenueById(venueId);
        venue.setState(state);
        venue.setUser(userId);
        return venueService.updateVenue(venue);
    }

    @ApiOperation("查询场地费用")
    @GetMapping("/venueCharges/{venueId}")
    public String venueCharges(@PathVariable int venueId){
        return venueService.venueCharges(venueId);
    }

    @ApiOperation("查询场地一周信息")
    @GetMapping("/weeklyInfo/{venueId}")
    public String weeklyInfo(@PathVariable int venueId){
        return venueService.weeklyInfo(venueId);
    }




    @ApiOperation("场地预定页面")
    @GetMapping("/venueBook/{venueId}")
    public String toVenueBook(@PathVariable int venueId, Model model){
        Venue venue = venueService.queryVenueById(venueId);
        model.addAttribute(venue);
        return "venue/venueBook";
    }

//    @ApiOperation("保存订单信息")
//    @PostMapping("/venueBook{startTime}{endTime}{venueId}")
//    public int venueBook(@PathVariable String startTime,@PathVariable String endTime,@PathVariable int venueId) throws ParseException {
//        return venueService.saveVenueOrder(startTime, endTime, venueId);
//    }

    @ApiOperation("预定场地页面")
    @GetMapping("/toVenueBooking")
    public String toVenueBooking(Model model){
        List<VenueBooking> venueBookings = venueService.queryVenueBooking();
        model.addAttribute("venueBookings",venueBookings);
        return "venue/venueBooking";
    }

    @ApiOperation("预定场地")
    @GetMapping("/saveVenueBooking/{venueBooking}")
    @ResponseBody
    public int venueBooking(@PathVariable VenueBooking venueBooking){
        return venueService.venueBooking(venueBooking);
    }

    @ApiOperation("取消预定")
    @GetMapping("/cancelBook/{userId}")
    @ResponseBody
    public int cancelBook(@PathVariable int userId){
        return venueService.cancelBook(userId);
    }

    @ApiOperation("开始计时")
    @PostMapping("/start")
    @ResponseBody
    public void start(@RequestBody VenueOrder venueOrder){
        System.out.println(venueOrder);
        venueService.newBooking(venueOrder);
    }

    @ApiOperation("结束计时")
    @PostMapping("/end")
    @ResponseBody
    public void end(@RequestParam("venueOrderId") int venueOrderId){
        venueService.endBooking(venueOrderId);
    }

    @ApiOperation("通过用户id查询计时场地")
    @GetMapping("/queryVenueOrderByUserId")
    @ResponseBody
    public List<VenueOrder> queryVenueOrderByUserId(@RequestParam("userId") int userId){
        return venueService.queryVenueOrderByUserId(userId);
    }

    @ApiOperation("通过用户id查询计时场地")
    @GetMapping("/queryAllVenueOrder")
    @ResponseBody
    public List<VenueOrder> queryAllVenueOrder(){
        return venueService.queryAllVenueOrder();
    }
}
